import Cookies from 'js-cookie'

const TOKEN_KEY = 'User-Token'
const USERNAME_KEY = 'User-Username'
const PASSWORD_KEY = 'User-Password'
const SERVER_KEY = 'User-Server'
const INTERNAL_SERVER_KEY = 'User-Internal-Server'

const storage = window.localStorage

export function getToken() {
  if (storage) {
    return storage.getItem(TOKEN_KEY)
  } else {
    return Cookies.get(TOKEN_KEY)
  }
}

export function setToken(token) {
  if (storage) {
    storage.setItem(TOKEN_KEY, token)
  } else {
    Cookies.set(TOKEN_KEY, token)
  }
}

export function removeToken() {
  if (storage) {
    storage.removeItem(TOKEN_KEY)
  } else {
    Cookies.remove(TOKEN_KEY)
  }
}

export function getUsername() {
  if (storage) {
    return storage.getItem(USERNAME_KEY)
  } else {
    return Cookies.get(USERNAME_KEY)
  }
}

export function setUsername(username) {
  if (storage) {
    storage.setItem(USERNAME_KEY, username)
  } else {
    Cookies.set(USERNAME_KEY, username)
  }
}

export function removeUsername() {
  if (storage) {
    storage.removeItem(USERNAME_KEY)
  } else {
    Cookies.remove(USERNAME_KEY)
  }
}

export function getPassword() {
  if (storage) {
    return storage.getItem(PASSWORD_KEY)
  } else {
    return Cookies.get(PASSWORD_KEY)
  }
}

export function setPassword(password) {
  if (storage) {
    storage.setItem(PASSWORD_KEY, password)
  } else {
    Cookies.set(PASSWORD_KEY, password)
  }
}

export function removePassword() {
  if (storage) {
    storage.removeItem(PASSWORD_KEY)
  } else {
    Cookies.remove(PASSWORD_KEY)
  }
}

export function getServer() {
  if (storage) {
    return storage.getItem(SERVER_KEY)
  } else {
    return Cookies.get(SERVER_KEY)
  }
}

export function getInternalServer() {
  if (storage) {
    return storage.getItem(INTERNAL_SERVER_KEY)
  } else {
    return Cookies.get(INTERNAL_SERVER_KEY)
  }
}

export function setServer(server) {
  const host = resolveHost(server)
  const internalHost = resolveInternalHost(server)

  if (storage) {
    storage.setItem(SERVER_KEY, host)
    storage.setItem(INTERNAL_SERVER_KEY, internalHost)
  } else {
    Cookies.set(SERVER_KEY, host)
    Cookies.set(INTERNAL_SERVER_KEY, internalHost)
  }
}

export function removeServer() {
  if (storage) {
    storage.removeItem(SERVER_KEY)
    storage.removeItem(INTERNAL_SERVER_KEY)
  } else {
    Cookies.remove(SERVER_KEY)
    Cookies.remove(INTERNAL_SERVER_KEY)
  }
}

export function resolveHost(server) {
  const servers = server.split(';')
  let host = ''
  if (servers.length === 2) {
    host = servers[0]
  } else if (servers.length === 1) {
    host = server
  }

  return host
}

export function resolveInternalHost(server) {
  const servers = server.split(';')
  let internalHost = ''
  if (servers.length === 2) {
    internalHost = servers[1]
  } else if (servers.length === 1) {
    internalHost = server.substring(server.indexOf('://') + 3).split(':')[0]
  }

  return internalHost
}

export function setUserAuth(username, password) {
  if (storage) {
    storage.setItem(username, password)
  } else {
    Cookies.set(username, password)
  }
}

export function getUserAuth(username) {
  if (storage) {
    return storage.getItem(username)
  } else {
    return Cookies.get(username)
  }
}
